package com.jingyu.sport.controllers;

import com.google.gson.Gson;
import com.qiniu.rtc.model.RoomAccess;
import com.qiniu.util.Auth;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController()
@RequestMapping("/live")
public class LiveController {
    private final static Logger logger = LogManager.getLogger(LiveController.class);

    //访问秘钥
    static private Auth auth = Auth.create("s-2eRj0LwLspctcISwo3UGxX5SxbrOV7jT1lmkQq", "O4s3Ck7yfsWw0OUi4jOTVYwqKxar9AzHefC7fGUB");
    static private String appId = "dksmehovx";

    @Autowired
    private Gson gson;

    @RequestMapping("/getRoomToken")
    //需走SSL
    String getRoomToken() throws Exception {
        String roomName = "testRoom";
        String userId = "testUser";
        //有效时间
        long expireAt = (System.currentTimeMillis() + 60 * 1000 * 24) / 1000;
        String permission = "user"; //user or admin

        RoomAccess access = new RoomAccess(appId, roomName, userId, expireAt, permission);

        return auth.signRoomToken(gson.toJson(access));
    }
}
